package com.tiandang.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tiandang.core.error.BusinessException;
import tiandang.core.error.EmBusinessError;
import tiandang.core.model.OrderInfo;
import tiandang.core.model.OrderProduct;
import tiandang.core.model.ProductStock;
import tiandang.core.service.ManagerService;
import tiandang.core.service.OrderService;
import tiandang.core.service.ProductService;
import tiandang.core.service.ProductStockService;

import java.util.List;

@Component
@EnableScheduling
public class Task {

  @Autowired
  private ManagerService managerService;

  @Autowired
  private OrderService orderService;

  @Autowired
  private ProductService productService;

  @Autowired
  private ProductStockService productStockService;

  //每周一凌晨1点执行,将团长的冻结佣金重置为0,加入到未结算佣金里面
  @Scheduled(cron = "0 0 1 ? * MON")
  public void resetFrozenRewardToNoBalance() {
    managerService.resetFrozenRewardToNoBalance();
  }

  //1.每分钟检查一下是否有未支付订单,未支付超过订单创建时间三分钟(180秒)的,默认为订单取消,并退回库存
  //2.检查已经支付但订单状态是已取消状态,将订单状态改成 0已创建,并减库存,加销量
  @Scheduled(cron = "0 */1 * * * ?")
  public void checkUnPayOrderAndCancel() throws BusinessException {
    //查询未支付的超过五分钟的订单列表
    List<OrderInfo> orderList = orderService.getUnPayOrder();
    for (OrderInfo order : orderList) {
      // 判断一下订单状态防止重复修改
      if (order.getOrderStatus() == 0) {
        dealCancelOrder(order);
      }
    }

    // bug 20200211 用户超过3分钟未支付,然后再支付,但订单状态已经改为了 已取消的bug
    //查询已经支付但订单状态是已取消状态的列表
    List<OrderInfo> orderList2 = orderService.getPayedAndCancelStatusOrder();
    for (OrderInfo order : orderList2) {
      dealPayedAndCancelStatusOrder(order);
    }

  }

  @Transactional
  private void dealCancelOrder(OrderInfo order) throws BusinessException {
    //将订单状态更新为 1
    orderService.updateOrderStatus(order.getId(), 1);

    List<OrderProduct> orderProductList = orderService.getOrderProductListByOrderId(order.getId());
    //加库存
    productService.increaseStock(orderProductList);

    //减去商品的销量
    productService.decreaseSales(orderProductList);
  }

  @Transactional
  private void dealPayedAndCancelStatusOrder(OrderInfo order) throws BusinessException {
    orderService.updateOrderStatus(order.getId(), 0);

    List<OrderProduct> orderProductList = orderService.getOrderProductListByOrderId(order.getId());

    //减库存
    ProductStock productStock;
    for (OrderProduct orderProduct : orderProductList) {
      productStock = productStockService.getProductStockByProductId(orderProduct.getProductId());
      if (productStock.getStock() < orderProduct.getProductNumber()) {
        throw new BusinessException(EmBusinessError.STOCK_NOT_ENOUGH);
      }
    }

    productService.decreaseStock(orderProductList);

    //加上商品的销量
    productService.increaseSales(orderProductList);
  }
}
